<?php
class category {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	var $lang_id=null;
	
	function category($lang_id) {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
		$this->lang_id=$lang_id;
    }
	
	function showTree()
	{
		$CatListArray = $this->fetchTree($this->lang_id);
		$keywordRstArray = $this->fetchKeywordList($this->lang_id);
		$keyWordArray = $this->getKeywordList($keywordRstArray);
		$html_str = $this->getCatList($CatListArray,1,$keyWordArray);
		$this->smarty->assign('html_str', $html_str );
		$this->smarty->assign('msg', $this->msg);
		
		$this->smarty->display( 'category.tpl' );
	}
	
	function fetchTree($lang_id)
	{
		$sql = " 
		  SELECT 
			cat1.category_id As catId1,cat1.category_name AS catName1, 
			cat2.category_id As catId2,cat2.category_name AS catName2, 
			cat3.category_id As catId3,cat3.category_name AS catName3, 
			cat4.category_id As catId4,cat4.category_name AS catName4 
		  FROM 
			category_mst cat1 
		  LEFT JOIN category_mst cat2 
			ON cat2.pid = cat1.category_id and cat1.pid=0 and cat1.del_flag=0 and cat2.del_flag=0 
		  LEFT JOIN category_mst cat3 
			ON cat3.pid = cat2.category_id and cat3.del_flag=0 
		  LEFT JOIN category_mst cat4 
		   ON cat4.pid = cat3.category_id and cat4.del_flag=0 
		  where cat1.del_flag=0 and cat1.pid=0 and cat1.lang_id=:lang_id 
		  order by catID1,catID2,catID3,catID4 ";
		$stmt = $this->db->prepare($sql);
		if (!$stmt) {
    		print_r($this->db->errorInfo());
		}
		$stmt->bindParam(':lang_id', $lang_id);
		$stmt->execute();
		$result = $stmt->fetchAll();
		$stmt = null;
		/*
		$CatListArray=array();
		
		foreach($result as $row)
		{
			array_push($CatListArray,$row);
		}
		*/
		return $result;
		
	}
	
	function fetchKeywordList($lang_id){
		$sql = " select tag.category_id as catId,tag_id as tag_id,tag_name as tag_name 
				 from tag_mst tag join category_mst 
				 	on tag.category_id=category_mst.category_id 
				 where  category_mst.del_flag=0 and category_mst.lang_id=:lang_id 
				 order by tag.category_id ";
		$stmt = $this->db->prepare($sql);
		if (!$stmt) {
    		print_r($this->db->errorInfo());
		}
		$stmt->bindParam(':lang_id', $lang_id);
		$stmt->execute();
		$result = $stmt->fetchAll();
		$stmt = null;
		
		/*
		$result = mysql_query($query, $link);
		
		$keywordRstArray=array();
		while ($row = mysql_fetch_object($result)) {
			array_push($keywordRstArray,$row);
		}*/

		return $result;
		
	}
	
	
	function getCatList($catArray,$level,$keyWordArray){	
		$catId = $catArray[0][$level*2-2];
		$catName = $catArray[0][$level*2-1];
		$index = 0;
		$catArrayNext = array();
		//global $keyWordArray;
		
		//print_r($keyWordArray);
		
		$htmlStr = "";
		foreach($catArray as $cat){
			if($level<=MAX_LEVLE && $catId <> $cat[$level*2-2]){
				$htmlStr .="<li>".$catName."\n";
				if(count($catArrayNext)>0){
					$htmlStr .=" <ul>";
					$htmlStr .= $this->getCatList($catArrayNext,$level+1,$keyWordArray);
					if(isSet($keyWordArray[$catId])){
						$htmlStr .= $keyWordArray[$catId];
					}
					$htmlStr .=" </ul>\n";
				}else{
					if(isSet($keyWordArray[$catId])){
						$htmlStr .= "<ul>".$keyWordArray[$catId]."</ul>\n";
					}else{
						$htmlStr .= " <ul><li>".KeyWordEmpty."</li></ul>\n";
					}
				}
				$htmlStr .= "</li>\n";
				unset($catArrayNext);
				$catId = $cat[$level*2-2];
				$catName = $cat[$level*2-1];
				$catArrayNext = array();
			}
			if($level<MAX_LEVLE && $cat[$level*2]<>null){
				array_push($catArrayNext,$cat);
			}
			
			$index ++;
			if($index == count($catArray)){
				$htmlStr .="<li>".$catName;
				if($level<MAX_LEVLE && count($catArrayNext)>0){
					$htmlStr .="<ul>";
					$htmlStr .= $this->getCatList($catArrayNext,$level+1,$keyWordArray);
					if(isSet($keyWordArray[$catId])){
						$htmlStr .= $keyWordArray[$catId];
					}
					$htmlStr .="</ul>\n";
				}else{
					if(isSet($keyWordArray[$catId])){
						$htmlStr .= "<ul>".$keyWordArray[$catId]."</ul>\n";
					}else{
						$htmlStr .= " <ul><li>".KeyWordEmpty."</li></ul>\n";
					}
				}
				$htmlStr .= "</li>\n";
			}
			
		}
		
		return $htmlStr;
	}
	
	function getKeywordList($keywordRstArray){
		$keyWordStr = "";
		$categoryId = 0;
		$keyWordArray = array();
		$index = 0;
		//<li><span style=\"color : #990203 \">".$key->key_id."</span> ".$key->keywordName."<li>
		foreach($keywordRstArray as $keyword){
			//print($keyword->catId);
			if($categoryId <> $keyword['catId']){
					$keyWordArray[$categoryId] = $keyWordStr;
				$categoryId = $keyword['catId'];
				$keyWordStr = "";
			}
			$keyWordStr .= "<li><span style=\"color : #990203 \">".$keyword['tag_id']."</span> ".$keyword['tag_name']."<li>\n";
			$index ++;
			if($index == count($keywordRstArray)){
				$keyWordArray[$categoryId]=$keyWordStr;
			}
		}
		return 	$keyWordArray;	
		
	}
	
	
	
	
	function initDropDown()
	{
		$sql = "select lang_id,lang_name from langMst where del_flag=0";
		foreach ($this->db->query($sql) as $row) {
    		$resultArr[$row['lang_id']]=$row['lang_name'];
		}
		
		$this->smarty->assign('langList', $resultArr);
		$this->fetchAddCatlist($this->lang_id);
	}
	
	
	function fetchAddCatlist($lang_id){

		$sql = "
		SELECT cat1.category_id AS catId,
		    cat2.category_name AS pcatName,
			cat1.category_name AS catName
		FROM  category_mst cat1
		    LEFT JOIN category_mst cat2
			ON  cat1.pid = cat2.category_id
              AND cat1.del_flag = 0
              AND cat2.del_flag = 0
		where cat1.del_flag=0 and cat1.lang_id=:lang_id
		ORDER BY cat2.category_id ";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':lang_id', $lang_id);
		$stmt->execute();
		$result = $stmt->fetchAll();
		$stmt = null;
		$this->smarty->assign('addCatList', $result);
	}
	
	function insertCat($formvars = array()){
		$pid=$formvars['pid'];
		$adlut_flag=$formvars['adult_flg'];
		$categoryList= split("\r\n",$formvars['category_name']);
		
		foreach($categoryList as $cat){
			$sql = "insert category_mst(pid,category_name,lang_id,adult_flag,createDate)
				    VALUES (:pid,:cat, :lang_id, :adlut_flag,now())";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(':pid', $pid);
			$stmt->bindParam(':cat', $cat);
			$stmt->bindParam(':lang_id', $this->lang_id);
			$stmt->bindParam(':adlut_flag', $adlut_flag);
			if($stmt->execute())
			{
				$this->msg = "カテゴリの登録に成功しました";
			}else
			{
				$this->msg = "カテゴリの登録に失敗しました";
			}
			$stmt = null;
		}
	}
	
	function delCat($formvars = array()){
		$category_id = $formvars['category_id'];

		$sql = "update category_mst set del_flag=1 where category_id=$category_id";
		
		
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "カテゴリ[$category_id]の削除に成功しました";
		}
		else
		{
			$this->msg = "カテゴリ[$category_id]の削除に失敗しました";
		}
	}
	
	function insertKeyWord($formvars = array()){
		$category_id = $formvars['category_id'];
		$keyword_nameList = split("\r\n",$formvars["keyword_name"]);	
		
		$sql = "SELECT * FROM category_mst WHERE category_id = :category_id";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':category_id', $category_id);
		$stmt->execute();
		$result = $stmt->fetch();
		if($result == null)
		{
			return;
		}

		$stmt = null;

		
		
		foreach($keyword_nameList as $keyword){
			$sql = "insert tag_mst(category_id,tag_name,lang_id,country_id)
				       VALUES (:category_id,:tagName,:lang_id,:country_id)";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(':category_id', $category_id);
			$stmt->bindParam(':tagName', $keyword);
			$stmt->bindParam(':lang_id', $result['lang_id']);
			$stmt->bindParam(':country_id', $result['country_id']);
			if($stmt->execute())
			{
				$this->msg = "タグ登録が完了しました";
			}else
			{
				$this->msg = "タグ登録に失敗しました";
			}
			$stmt = null;
		}
	}
	
	function delKeyWord($formvars = array()){
		$tag_id = $formvars['tag_id'];

		$sql = "delete from tag_mst where tag_id=:tag_id";
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(':tag_id', $tag_id);
		if($stmt->execute())
		{
			$this->msg = "指定タグの削除に成功しました！";
		}else
		{
			$this->msg = "指定タグは存在しません！";
		}
		$stmt = null;
	}
}
?>